package com.itboy.mall.pms.productattrvalue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.productattrvalue.bo.MallProductAttrValueAddBo;
import com.itboy.mall.pms.productattrvalue.bo.MallProductAttrValueQueryBo;
import com.itboy.mall.pms.productattrvalue.bo.MallProductAttrValueEditBo;
import com.itboy.mall.pms.productattrvalue.domain.MallProductAttrValue;
import com.itboy.mall.pms.productattrvalue.mapper.MallProductAttrValueMapper;
import com.itboy.mall.pms.productattrvalue.vo.MallProductAttrValueVo;
import com.itboy.mall.pms.productattrvalue.service.IMallProductAttrValueService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * spu属性值Service业务层处理
 *
 * @author itboy
 * @date 2021-08-07
 */
@Service
public class MallProductAttrValueServiceImpl extends ServicePlusImpl<MallProductAttrValueMapper, MallProductAttrValue> implements IMallProductAttrValueService {

	@Autowired
	private IMallProductAttrValueService iMallProductAttrValueService;
    @Override
    public MallProductAttrValueVo queryById(Long id){
        return getVoById(id, MallProductAttrValueVo.class);
    }

    @Override
    public TableDataInfo<MallProductAttrValueVo> queryPageList(MallProductAttrValueQueryBo bo) {
        PagePlus<MallProductAttrValue, MallProductAttrValueVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallProductAttrValueVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallProductAttrValueVo> queryList(MallProductAttrValueQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallProductAttrValueVo.class);
    }

    private LambdaQueryWrapper<MallProductAttrValue> buildQueryWrapper(MallProductAttrValueQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallProductAttrValue> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSpuId() != null, MallProductAttrValue::getSpuId, bo.getSpuId());
        lqw.eq(bo.getAttrId() != null, MallProductAttrValue::getAttrId, bo.getAttrId());
        lqw.like(StrUtil.isNotBlank(bo.getAttrName()), MallProductAttrValue::getAttrName, bo.getAttrName());
        lqw.eq(StrUtil.isNotBlank(bo.getAttrValue()), MallProductAttrValue::getAttrValue, bo.getAttrValue());
        lqw.eq(bo.getAttrSort() != null, MallProductAttrValue::getAttrSort, bo.getAttrSort());
        lqw.eq(bo.getQuickShow() != null, MallProductAttrValue::getQuickShow, bo.getQuickShow());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallProductAttrValueAddBo bo) {
        MallProductAttrValue add = BeanUtil.toBean(bo, MallProductAttrValue.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallProductAttrValueEditBo bo) {
        MallProductAttrValue update = BeanUtil.toBean(bo, MallProductAttrValue.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallProductAttrValue entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public List<MallProductAttrValue> getListBySpuId(Long spuId) {
		LambdaQueryWrapper<MallProductAttrValue> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallProductAttrValue::getSpuId,spuId);
		return this.baseMapper.selectList(queryWrapper);
	}

	@Override
	public boolean updateBatch(Long spuId,List<MallProductAttrValue> bo) {
		//1. 删除这个spuId之前对应的所有属性
		this.baseMapper.delete(new LambdaQueryWrapper<MallProductAttrValue>().eq(MallProductAttrValue::getSpuId,spuId));

		List<MallProductAttrValue> collect = bo.stream().map(item -> {
			item.setSpuId(spuId);
			return item;
		}).collect(Collectors.toList());
		//2.保存
		return this.saveBatch(collect);
	}
}
